package com.example.demo01.demo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.demo01.R;
import com.example.demo01.fragments.TitleFragment;
import com.example.demo01.utils.Utils;

public class HandlerMessageDemo extends AppCompatActivity {
    Utils utils = new Utils(this);

    //! 为什么要用这个传递？，因为在子线程中不能直接操作页面数据只能在主线程中操作页面数据
    private MyHandler myHandler = new MyHandler();

    public static final int WHAT_VIEW = 1; //. 约定主子线程传递消息码

    Button btn_download;

    ProgressBar progressBar;
    TextView tv_progressBar_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_handler_message);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        //. 添加标题
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_title, TitleFragment.newInstance(getResources().getString(R.string.HandlerMessageDemo)))
                .commit();

        initView();
    }

    //. 初始化视图
    private void initView() {
        btn_download = findViewById(R.id.btn_download);
        progressBar = findViewById(R.id.progressBar);
        tv_progressBar_text = findViewById(R.id.tv_progressBar_text);


        addListener();
    }

    //. 添加点击事件
    private void addListener() {
        btn_download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                utils.showToast("开始下载");
                //. 创建子线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 1; i <= 100; i++) {
                            try {
                                Thread.sleep(50);//. 模拟网络请求
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }

                            Message message = myHandler.obtainMessage(); //. 创建消息
                            message.what = WHAT_VIEW; //. 约定主子线程传递消息码
                            message.obj = i; //. 设置要传递的数据
                            myHandler.sendMessage(message);//. 发送消息
                        }
                    }
                }).start();
            }
        });
    }

    class MyHandler extends Handler {
        //. 处理消息
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == WHAT_VIEW) {
                int process = (int) msg.obj;
                progressBar.setProgress(process);
                tv_progressBar_text.setText(process + "/100");
                if (process == 100) {
                    utils.showToast("下载完毕");
                }
            }
        }
    }
}